package com.wangge.concurrentTest;

import java.io.PrintStream;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicBarrierTest {
  public static void main(String[] args) {
    CyclicBarrier barrier = new CyclicBarrier(4);
    ExecutorService exec = Executors.newCachedThreadPool();

      exec.execute(() -> {
        runOne(barrier);
      });
      exec.execute(() -> runOne(barrier));
      exec.execute(() -> runOne(barrier));
      exec.execute(() -> runOne(barrier));
      System.out.println("第一次" + barrier.getNumberWaiting());

      exec.execute(() -> System.out.println("sdf"));
  }

  private static void runOne(CyclicBarrier barrier) {
    try {
      System.out.println("sdf");
      barrier.await();
      System.out.println("完成一个");

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}
